<?php
if($_SESSION['user_id_sess']=="") {
		echo "<script type='text/javascript'> window.location='index.php?signin&url=mylist'; </script>";
	}
require_once 'includes/class.phpmailer.php';

$proId=$_REQUEST['Postfeedback'];
$order_id=$_REQUEST['order_id'];
if($_REQUEST['type']=='0')
{
$sqlSelProd="SELECT * FROM tbl_product WHERE product_id='".$proId."'";
}
else
{
 $sqlSelProd="SELECT * FROM tbl_wholeseller_product WHERE product_id='$proId'";
}

$rstSelProd=mysql_query($sqlSelProd);
$rowSelProd=mysql_fetch_array($rstSelProd);
$productOwner=$rowSelProd['posted_by'];	
$productName=$rowSelProd['product_name'];	
$productDesc=$rowSelProd['product_description'];
$productCategory=$rowSelProd['category_id'];
$productPrice=$rowSelProd['product_price'];
$postedDate=$rowSelProd['posted_date'];
$sellingType=$rowSelProd['selling_type'];
$shipping = number_format($rowSelProd['shipping'],2);
$shipping_display = number_format($rowSelProd['shipping'],0);

if(!empty($shipping) && $shipping!=0.00)
 $shipping="$".$shipping;
else
 $shipping="Free";
$pimage = getProductImage($proId,'p');
 //echo $pthumb;
if($pimage['thumb']!="") {
	$productThumbnail = $pimage['thumb'];
 }  else {
	$productThumbnail="images/no_image.gif";	
 } 
		  
$catInfo=getCategory($productCategory);
//get user name using function
$proOwnerName = getUser($rowSelProd['posted_by']);

//check user already selling this item or not
$sqlSelItem = "SELECT item_id FROM tbl_items WHERE product_id='$proId' 
			   AND owner_id='$productOwner'";
$rstSelItem = mysql_query($sqlSelItem) or die(mysql_error());			   
$numSelItem = mysql_num_rows($rstSelItem);
$arrSelItem=mysql_fetch_array($rstSelItem);
//$ownname = getUser($productOwner);
$owner_arr=mysql_fetch_array(mysql_query("select * from tbl_users where user_id='".$productOwner."'"));
$ownname = $owner_arr['first_name'];

if($_SESSION['user_id_sess']==$productOwner)
{
	//$ownname =getUser($_REQUEST['order_by']);
	$ownname = $owner_arr['first_name'];
	$productOwner=$_REQUEST['order_by'];	
}


if(isset($_POST['post_feedback']) && $_POST['post_feedback']=="PostFeedback")
{
	$comment=$_REQUEST['comment'];
	$rate=$_REQUEST['rate'];
	$comment_to=$_REQUEST['comm_to'];
	$comment_by=$_SESSION['user_id_sess'];
	$post_date=date('Y-m-d');
	if($rate==1)
	{
	  $points=100;
	}
	else if($rate==3)
	{
	  $points=50;
	}
	//echo "INSERT INTO tbl_feedback set option='$rate',comment='$comment',comm_to='$comment_to',comm_by='$comment_by'";
	$ins_feed=mysql_query("INSERT INTO tbl_feedback values('','$comment','$comment_to','$comment_by','$post_date','$rate','$points','$order_id','$proId')") or die(mysql_error());
	//echo "INSERT INTO tbl_feedback values('','$comment','$comment_to','$comment_by','$post_date','$rate','$order_id')";
	//exit;
	//echo "<div style='color:red;margin-left:10px;'>Your Feedback posted successfully!</div>";
	header("location: index.php?Postfeedback=".$_REQUEST['Postfeedback']."&comm_to=".$_REQUEST['comm_to']."&type=".$_REQUEST[
	'type']."&order_id=".$_REQUEST['order_id']."&owner_id=".$_REQUEST['owner_id']."&msg=succ");
}

$selgrtcnt1=mysql_query("select count(*) as grtcnt from tbl_feedback where comm_to='".$_REQUEST['owner_id']."' AND rate='1' AND DATEDIFF(post_on,date('Y-m-d')<=12)");
	$rowgrtcnt1=mysql_fetch_array($selgrtcnt1);
	$grtcnt1=$rowgrtcnt1['grtcnt'];
	
				
	$selnegcnt1=mysql_query("select count(*) as negcnt from tbl_feedback where comm_to='$userId' AND rate='3' AND DATEDIFF(post_on,date('Y-m-d')<=12)");
	$rownegcnt1=mysql_fetch_array($selnegcnt1);
	$negcnt1=$rownegcnt1['negcnt'];
	
	$selnotrans1=mysql_query("select count(*) as notrans from tbl_feedback where comm_to='".$_REQUEST['owner_id']."' AND DATEDIFF(post_on,date('Y-m-d')<=12)");
	$rowtranscnt1=mysql_fetch_array($selnotrans1);
	$notrans1=$rowtranscnt1['notrans'];
	
	if($grtcnt1!=0 && $negcnt1!=0)
	$feedbackcnt1=$grtcnt1/($grtcnt1+$negcnt1);
	else
	$feedbackcnt1=0;
	
	if($notrans1!=0)
	$percentfeedback1=(100*$feedbackcnt1)/$notrans1;
	else
	$percentfeedback1=100;
?>

<script language="javascript">
  function funpostfeedback()
	{
		if(document.getElementById("comment").value=="")
		{
			alert("Please enter comment.");
			return false;
		}
		else{
		var confirm_feedback = confirm('are you sure this is the feedback you would like to leave?');
		if(confirm_feedback){
		return true;
		}
		else
		{
		return false;
		}
		}
		  
	}

</script>
<style type="text/css">
	span#add-wishlist { color:#0000CC; padding:5px; font-size:12px; }	
</style>
<?php if(isset($_GET['msg']) && $_GET['msg']=="succ") { ?>
<div style='color:red;margin-left:10px;'>Your Feedback posted successfully!</div>
<?php } ?>
<div id='panel'>
<div class="fulldiv" style="width:950px;">

<div class="fulldiv" style="margin:10px 0px 0px 0px;">
	
<div style="float:left;"><strong class="orangehead">Your Feedback On Transaction </strong></div>
<p class="pro_ptag grey_txt">Please leave honest feedback on your transaction.</p>

<div class="fulldiv">
<div id="other1_feedback_pending_merc" class="greytab_out" style="float:left; margin:0px 5px -11px 12px;">
	<div class="whttab_left">&nbsp;</div>
	<div class="whttab_repeat"><a href="<?=$base_url?>index.php?feedback=1&owner_id=<?=$_REQUEST['owner_id']?>"><?=$ownname?></a>&nbsp;&nbsp;							<?php
													$numTotalFeedback=mysql_num_rows(mysql_query("select * from tbl_feedback where product_id='".$proId."'"));
													$sum_of_positive=mysql_fetch_array(mysql_query("select SUM(points) as total_positive from tbl_feedback where product_id='".$proId."' and rate=1"));
                                                    $sum_of_negative=mysql_fetch_array(mysql_query("select SUM(points) as total_negative from tbl_feedback where product_id='".$proId."' and rate=3"));													                                                    $totalTransaction=mysql_num_rows(mysql_query("select * from tbl_feedback where product_id='".$proId."' and rate!=2"));
													$feedback_rate=($sum_of_positive['total_positive']+$sum_of_negative['total_negative'])/$totalTransaction;
													if($numTotalFeedback>0 && $totalTransaction>0)
													{
													echo number_format($feedback_rate,2).'%'. " (".$numTotalFeedback.")";
													}
													else if($numTotalFeedback==0 && $totalTransaction==0)
													{
													echo '100%'. " (0)";
													}
													else
													{
													echo '100%'. " (1)";
													}
													?>
</div>
	<div class="whttab_right">&nbsp;</div>
</div>


<table width="100%" border="0">
  
<tr>
	<td>	
	   <form name="frmProductInfo" id="frmProductInfo" method="post">
	   <input type="hidden" name="hidBargain" id="hidBargain">
<div class="fulldiv">
	   	<div class="fullbox2_top">&nbsp;</div>
		<div class="fullbox2_repeat">
		<div class="fdbk_box"> 	
			 <table width='100%' border='0' align='center' cellspacing='0' cellpadding="0" >
			   <tr>
				 <td width="15%" rowspan="2" style='vertical-align:middle; padding:5px; text-align:center'><img src="<?php echo $productThumbnail; ?>" /><br /><br><a href="javascript:void(0);" onClick="openWindow('item_viewer.php?prod=<?php echo $proId; ?>&type=p','580','650');"><?php if(getImageCount($proId)!=0) echo getImageCount($proId)." images"  ?></a> <div>
				 </div></td>
				 <td width="60%" style='vertical-align:top; padding:5px;'>
				 <a href="index.php?products=<?php echo $arrSelItem['item_id']?>"><a href="index.php?products=<?php echo $arrSelItem['item_id']?>"><font color="#b90006" size="3"><strong><?php echo "$productName";?></strong></font></a>
				 <p class="pro_ptag"><?php if($_SESSION['user_id_sess']=="") { $usr=0; } else { $usr=$_SESSION['user_id_sess'];} ?>
				 	<span id="divAddItemToSeller"></span>
				 </p>
				 <p class="pro_ptag">
					<?php 
					
					$owntype = printUserType($productOwner);
					
					//$ownname = getUser($productOwner);
					if($_SESSION['user_id_sess']==$productOwner)
						echo "<strong> Buyer </strong> : ".ucfirst($ownname);
					else
						echo "<strong> Product Owner </strong> : ".ucfirst($ownname);
					
					?> 
				 </p>
				 <p class="pro_ptag grey_txt">local pick up - <font style="color:#000000;">not available</font></p>
				 </td>
				 <td width="20%" style='vertical-align:top; padding:5px;'>
				 <table width="100%" border="0" cellpadding="0"  cellspacing='0'>
					 <td>
					 <p class="redtxt" style="margin: 5px 0px; font-size: 12px; text-align:right;"><font style="color:#000000;"><strong>Price - </strong></font>$<?php echo number_format($productPrice,0); ?>
						<font style="color:#000000;">&nbsp;+&nbsp;$<?php echo $shipping_display ?> s&h</font>
					 </p>
					 </td>
				 </table>
				  </td>
				</tr>
			   
			   <tr>
				 <td height="25" colspan="2" ><!--<a href="javascript:void(0);" onClick="openWindow('item_viewer.php?prod=<?php echo $proId; ?>&type=p','580','650');"><?php //if(getImageCount($proId)!=0) echo getImageCount($proId)." images"  ?></a>--></td>
			   </tr>
			 </table>		
		</div>	
		</div>
		<div class="fullbox2_bot">&nbsp;</div>
</div>

<div class="fulldiv">	
<p class="pro_ptag"><strong>Rate this transaction</strong></p>			 
		<div class="fullbox2_top">&nbsp;</div>
		<div class="fullbox2_repeat">
		<div class="fdbk_box"> 
			<table width="100%" align="center" border="0">

					<tr>
						<td>
							<table width="100%" border="0" >
								<tr>
								  <td colspan="3">
									<p class="grey_txt pro_ptag">Your Options</p>
								  </td>
								</tr>
							<tr>
							  <td width="5%">
								<input type="radio" name="rate" checked="checked" id="rate1" value="1" onclick="click_feed_great();" >
							  </td>
							  <td width="5%">
								<img src="images/tick_blue.png" />
							  </td>
							  <td width="85%">
								Great
							  </td>
							</tr>
							<tr>
							  <td width="5%">
								<input type="radio" name="rate" id="rate2" value="2" onclick="click_feed_accep();">
							  </td>
							  <td width="5%">
								<img src="images/accept.png" />
							  </td>
							  <td width="85%">
								Acceptable
							  </td>
							</tr>
							<tr>
							  <td width="5%">
								<input type="radio" name="rate" id="rate3" value="3" onclick="click_feed_negative();">
							  </td>
							  <td width="5%">
									<img src="images/delete.png" />
							  </td>
							  <td width="85%">
								Negative
							  </td>
							</tr>
							<script language="javascript">
							  $('#rate'+<?=$_REQUEST['rate']?>).attr('checked','checked');
							</script>
							</table>
						</td>
					</tr>
				</table>
		</div>				
		</div>
		<div class="fullbox2_bot">&nbsp;</div>
</div>	
		
<div class="fulldiv">
<p class="pro_ptag"><strong>Comments on this transaction </strong></p>		
		<div class="fullbox2_top">&nbsp;</div>
		<div class="fullbox2_repeat">
		<div class="fdbk_box"> 			
			<table width="100%" border="0" align="center">
			<tr>
			   <td >
			<table  border="0" align="center" width="100%">              
				   <tr>
					 <td width="93%" height="28" colspan="2" class="uline">
			   <table align="center" width="90%" border="0">
						<tr>
							<td width="5%">
							<span id="span_feedback_image"><img src="images/tick_blue.png" /></span>
							</td>
							<td width="85%">
				<textarea name="comment" id="comment" style="height:150px; width:650px; " class="inputbg"><?=$comment?></textarea>
				<input type="hidden" name="owner_id" value="<?php echo $productOwner;?>" >
							</td>
						</tr>					
				</table>
				</td>
			  </tr>
			</table>
					 </td>
				   </tr>
				 </table>		
		</div>		 
		</div>
		<div class="fullbox2_bot">&nbsp;</div>
</div>

<div class="fulldiv">
<p style="text-align:center;">
	<input type="submit"  name="btnCommit" value="Submit feedback" onClick="return funpostfeedback()" class="refer_btn" style="margin:0px;">
</p>	
<p style="text-align:center;" class="grey_txt">
	Your homest feedback is very important and appreciated.
</p>
</div>	
		<input type="hidden" name="post_feedback" value="PostFeedback" />
		</form>
	</td>
</tr>          
</table> 
	 	</div>  
		
<script language="javascript" type="text/javascript">
function click_feed_great()
{
 document.getElementById('span_feedback_image').innerHTML='<img src="images/tick_blue.png">';
}
function click_feed_accep()
{
 document.getElementById('span_feedback_image').innerHTML='<img src="images/accept.png">';
}
function click_feed_negative()
{
 document.getElementById('span_feedback_image').innerHTML='<img src="images/delete.png">';
}
</script>
    
